#!/usr/bin/env python
# -*- coding: utf-8 -*-

# define level  off/on/all/debug/info/warn/error/fatal
# off  不打印不记录
# on  只打印，不记录到文件
# all 打印所有信息，并记录到文件
# debug，info，warn， error， fatal  不同的日志级别

__author__ = 'dalon'
import sys, os
from datetime import datetime

class logger(object):
    def __init__(self, logfile, level):
        self.levels = {'off': -1, 'on': 0, 'all':1, 'debug': 2, 'info':3, 'warn':4, 'error': 5, 'fatal': 6}
        if level not in self.levels.keys():
            sys.exit()
        self.level = self.levels[level]
        if self.level > 0:
            if not os.path.exists('log'):
                os.mkdir('log')
            self.logfile = open('.%slog%s' % (os.sep, os.sep) + logfile, 'w')
        else:
            pass
        self.ifprint=1
    def set_print_off(self):
        self.ifprint=0
    def set_print_on(self):
        self.ifprint=1
    def on(self, msg):
        # level 0
        print str(datetime.now()) + '  ' + msg
    def all(self, msg):
        # level 1
        if self.level <= 1 and self.level>=0:
            if self.ifprint:
                print '\033[0;32;m' + str(datetime.now()) + '  ALL         ', msg, '\033[0m'
                #print str(datetime.now()) + '  ALL         ' + msg
            self.record(str(datetime.now()) + '  ALL         ' + msg)
    def debug(self, msg):
        # level 2
        if self.level <= 2 and self.level>=0:
            if self.ifprint:
                #print str(datetime.now()) + '  DEBUG       ' + msg
                print '\033[0;32;m' + str(datetime.now()) + '  DEBUG       ', msg, '\033[0m'
            self.record(str(datetime.now()) + '  DEBUG       ' + msg)
    def info(self, msg):
        # level 3
        if self.level <= 3 and self.level>=0:
            if self.ifprint:
                #print str(datetime.now()) + '  INFO        ' + msg
                print '\033[0;34;m' + str(datetime.now()) + '  INFO        ', msg, '\033[0m'
            self.record(str(datetime.now()) + '  INFO        ' + msg)
    def warn(self, msg):
        # level 4
        if self.level <= 4 and self.level>=0:
            if self.ifprint:
                #print str(datetime.now()) + '  INFO        ' + msg
                print '\033[0;33;m' + str(datetime.now()) + '  WARN        ', msg, '\033[0m'
            self.record(str(datetime.now()) + '  WARN        ' + msg)
    def error(self, msg):
        # level 5
        if self.level <= 5 and self.level>=0:
            if self.ifprint:
                #print str(datetime.now()) + '  ERROR       ' + msg
                print '\033[0;31;m' + str(datetime.now()) + '  ERROR       ', msg, '\033[0m'
            self.record(str(datetime.now()) + '  ERROR       ' + msg)
    def fatal(self, msg):
        # level 6
        if self.level <= 6 and self.level>=0:
            if self.ifprint:
                #print str(datetime.now()) + '  FATAL       ' + msg
                print '\033[0;35;m' + str(datetime.now()) + '  FATAL       ', msg, '\033[0m'
            self.record(str(datetime.now()) + '  FATAL       ' + msg)
    def record(self, msg):
        if self.level > 0:
            self.logfile.write(msg)
            self.logfile.flush()
        else:
            pass
    def exit(self):
        if self.level > 0:
            self.logfile.close()
        else:
            pass


if __name__ == '__main__':
    a = logger('result1.txt', 'all')
    #a.set_print_off()
    a.on('log on')
    a.all('log all')
    a.debug('log debug')
    a.info('log info')
    a.warn('log warn')
    a.error('log error')
    a.fatal('log fatal')
    a.exit()
